<?php
/**
 * @file $HeadURL: tryapply.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2015-3-24 下午7:35:32 $
 * @brief 
 */
namespace haibao\cms\data;


class WxUser extends \haibao\cms\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\cms\model\data\WxUser');
	}
    
	/**
	 * 根据主键id查询微信用户
	 * @param int $Id
	 * @return object
	 */
	public function getWxUserById($Id){
		return $this->getOneById($Id);
	}
	
	/**
	 * 根据微信用户ID查询微信用户
	 */
	public function getUserByWxId($wxId){
		$result = $this->query('select * from WxUser where WxId=%s', array($wxId));
		return $result->fetch_assoc();
	}
	
	/**
	 * 保存微信用户
	 * @param object $model
	 */
	public function saveUser($model){
	    $model->Id ? $this->updateWxUserById($model) : $this->addWxUser($model);
	}
	
	/**
	 * 插入微信用户
	 */
	private function addWxUser($model){
	    $this->add($model);
	}
	
	/**
	 * 更新微信用户信息
	 * @param \haibao\cms\model\data\WxUser $model
	 */
	private function updateWxUserById($model){
	    $this->updateById($model->Id, array(
	        \haibao\cms\model\filter\WxUser::CONDITION_FIELD_NAME_USERNAME => $model->UserName,
	        \haibao\cms\model\filter\WxUser::CONDITION_FIELD_NAME_REASON => $model->Reason,
	        \haibao\cms\model\filter\WxUser::CONDITION_FIELD_NAME_IP => $model->IP,
	        \haibao\cms\model\filter\WxUser::CONDITION_FIELD_NAME_PHONE => $model->Phone,
	        \haibao\cms\model\filter\WxUser::CONDITION_FIELD_NAME_ADDTIME => $model->AddTime,
	    ));
	}
    
}